﻿using Core.Ndatax.Domain;
using Core.Ndatax.Infrastructure.Common;
using Core.Ndatax.Infrastructure.Entitys;
using Core.Ndatax.Model;
using Microsoft.AspNetCore.Mvc;
using Xman.Extensions.Core;

namespace Core.Ndatax.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RisController : ControllerBase
    {
        private readonly ILogger<RisController> _logger;
        private readonly IBllRequest _bllRequest;
        private readonly RisServer _risServer;

        public RisController(ILogger<RisController> logger, IBllRequest bllRequest, RisServer risServer)
        {
            _logger = logger;
            _bllRequest = bllRequest;
            _risServer = risServer;
        }

        /// <summary>
        /// 报到接口
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<CheckInOutput> CheckInAsync(CheckInInput input)
        {
            _logger.LogInformation($"报到入参:{input.ToJson()}");
            try
            {
                var dtoList = await _bllRequest.GetCheckInOrRegisterDtoAsync(input.SheetIDs, EnumRisOperateFlag.CheckIn);
                if (!dtoList.Any())
                {
                    _logger.LogInformation("存储获取的数据为空，所以不进行报到操作");
                    return new CheckInOutput() { Result = true };
                }

                var dtoinput = MapperHelper.CreateCheckInOrRegisterDto(dtoList, EnumRisOperateFlag.CheckIn);
                var result = await _risServer.CheckInAsync(dtoinput);
                if (!string.IsNullOrEmpty(result.Error))
                {
                    _logger.LogInformation("返回的resul异常，抛出异常----------------------------------" + result.Error);
                    throw new Exception(result.Error);
                }
                _logger.LogInformation($"报到结束");
            }
            catch (Exception ex)
            {
                _logger.LogInformation("报到异常，" + ex);
                return new CheckInOutput() { Result = false, Error = ex.Message };
            }
            return new CheckInOutput() { Result = true };
        }

        /// <summary>
        /// 取消报到接口
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<CancelCheckInOutput> CancelCheckInAsync(CancelCheckInInput input)
        {
            _logger.LogInformation($"取消报到入参:{input.ToJson()}");
            try
            {
                var dtoList = await _bllRequest.GetCancelRegisterDtoAsync(input.SheetIDList);
                if (!dtoList.Any())
                {
                    _logger.LogInformation("取消报到，存储获取的数据为空，所以不进行取消登记操作");
                    return new CancelCheckInOutput() { Result = true };
                }

                var dtoinput = MapperHelper.CreateCancelRegisterDto(dtoList, input.SheetIDList);
                var result = await _risServer.CancelRegisterOrCheckInAsync(dtoinput);
                if (!string.IsNullOrEmpty(result.Error))
                {
                    throw new Exception(result.Error);
                }
                _logger.LogInformation($"取消报到完成");
            }
            catch (Exception ex)
            {
                _logger.LogInformation("取消报到异常，" + ex);
                return new CancelCheckInOutput() { Result = false, Message = ex.Message };
            }
            return new CancelCheckInOutput() { Result = true };
        }

    }
}
